package com.chentf.rabbitmq.workquene03;

import com.chentf.rabbitmq.utils.RabbitMqUtilsSGG;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
 * 消费者2
 * */
public class Worker04 {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtilsSGG.getChannel();
        System.out.println("C2等待接受消息处理时间较短");
        DeliverCallback deliverCallback = (consumerTag,message) -> {
            // 沉睡30秒
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("接受到的消息是:"+new String(message.getBody()));
            //进行手动应答
            /*
             * 参数1：消息的标记  tag
             * 参数2：是否批量应答，false：不批量应答 true：批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        // 设置不公平分发
        // int prefetchCount = 1;
        //channel.basicQos(prefetchCount);

        //预设值是5
        int prefetchCount = 5;
        channel.basicQos(5);

        //采用手动应答
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME,autoAck,deliverCallback,(consumerTag) -> {
            System.out.println(consumerTag+"消费者取消消费接口回调逻辑");
        });
    }
}
